package com.example.course.converter;

import com.example.course.bean.Topic;
import com.example.course.dto.TopicDTO;
import com.example.course.vo.TopicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 题库转换器
 *
 * @author Jasmine-drs
 * @date 2025-04-22
 */
@Component
public class TopicConverter {

	/**
	 * 将DTO转换为实体类
	 *
	 * @param dto 题目DTO
	 * @return 题目实体
	 */
	public Topic toEntity(TopicDTO dto) {
		if (dto == null) {
			return null;
		}
		Topic entity = new Topic();
		BeanUtils.copyProperties(dto, entity);
		return entity;
	}

	/**
	 * 将实体类转换为VO
	 *
	 * @param entity 题目实体
	 * @return 题目VO
	 */
	public TopicVO toVO(Topic entity) {
		if (entity == null) {
			return null;
		}
		TopicVO vo = new TopicVO();
		BeanUtils.copyProperties(entity, vo);

		// 设置类型文本
		if (entity.getType() != null) {
			switch (entity.getType()) {
				case 0:
					vo.setTypeText("单选题");
					break;
				case 1:
					vo.setTypeText("填空题");
					break;
				default:
					vo.setTypeText("其他");
			}
		}

		return vo;
	}

	/**
	 * 批量转换实体类为VO
	 *
	 * @param entityList 实体列表
	 * @return VO列表
	 */
	public List<TopicVO> toVOList(List<Topic> entityList) {
		if (entityList == null) {
			return null;
		}
		List<TopicVO> voList = new ArrayList<>(entityList.size());
		for (Topic entity : entityList) {
			voList.add(toVO(entity));
		}
		return voList;
	}
} 